/**
 * 
 */
package neptune.manager.configurator;

import neptune.manager.model.topology.Property;

/**
 * @author Roberto Bifulco [info@robertobifulco.it, robertobifulco.it]
 * 
 */
public class SSHCommandsUtils {

	public static String getAddDelayCommand(String device, int delay) {
		return "tc qdisc add dev " + device + " root netem delay " + delay
				+ "ms";
	}

	public static String getDeleteQdicsCommand(String device) {
		return "tc qdisc del dev " + device + " root";
	}

	public static String getGenericNetemPropertyCommand(String device,
			String property, String value) {
		return "tc qdisc change dev " + device + " root netem " + property
				+ " " + value;
	}

	public static String getCreateNetemQdiscCommand(String device) {
		return "tc qdisc add dev " + device + " root netem";
	}

	/**
	 * Restituisce il comando per impostare tutti i paramatrei netem sul dato
	 * device
	 * 
	 * @param device
	 * @param properties
	 * @return
	 */
	public static String getCreateQdiscCommand(String device,
			Property[] properties) {
		String command = "tc qdisc add dev " + device + " root handle 1: netem";
		for (int i = 0; i < properties.length; i++) {
			if (properties[i].getName().equals(Property.BANDWIDTH))
				continue;
			command += " " + properties[i].getName() + " "
					+ properties[i].getValue();
		}
		return command;
	}

	/**
	 * Ottiene i due comandi necessari a configuare la bandwidth con coda HTB.
	 * Questi comandi vanno sempre eseguiti dopo aver configurato una coda netem
	 * come root
	 * 
	 * @param device
	 * @param bandwidth
	 * @return
	 */
	public static String[] getHTBCommands(String device, String bandwidth) {
		String[] commands = new String[2];

		commands[0] = "tc qdisc add dev " + device
				+ " parent 1: handle 10: htb default 1";
		commands[1] = "tc class add dev " + device
				+ " parent 10: classid 10:1 htb rate " + bandwidth;

		return commands;
	}

	public static String getRouteCommand() {
		return "route -n";
	}
	
	public static String getAddDefaultGatewayCommand(String ipGateway)
	{
		return new String("route add default gw "+ipGateway);
	}

	public static String getDelRouteCommand(String[] route) {
		String destAddress = route[0];
		// String gateway = route[1];
		String netmask = route[2];
		String flags = route[3];
		// String metric = route[4];
		// String ref = route[5];
		// String use = route[6];
		// String iface = route[7];

		boolean isHost = flags.contains("H");

		String command = "route del " + ((isHost) ? "" : "-net ") + destAddress
				+ ((isHost) ? "" : " netmask " + netmask);
		return command;
	}

	public static String getAddRouteCommand(String destinationAddress,
			String netmask, String outInterface) {
		String command = "route add -net " + destinationAddress + " netmask "
				+ netmask + " " + outInterface;

		return command;
	}

	public static String getSetInterfaceIP(String ifName, String address,
			String subnetMask) {
		String command = "ifconfig " + ifName + " " + address + " netmask "
				+ subnetMask + " up";

		return command;
	}

}
